package com.seeyon.apps.seepkk.form.workflowevent;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.seeyon.apps.kk.constants.Constants;
import com.seeyon.apps.kk.form.workflowevent.KkBaseExtendPlug;
import com.seeyon.apps.kk.util.JdbcAgentUtil;
import com.seeyon.apps.seepkk.manager.QiangsSeepManager;
import com.seeyon.apps.seepkk.manager.SeepManager;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormDataMasterBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.constants.ApplicationCategoryEnum;
import com.seeyon.ctp.workflow.event.WorkflowEventData;
import com.seeyon.ctp.workflow.event.WorkflowEventResult;
public class SeepIOKkExtendPlug extends KkBaseExtendPlug {
	private static final Log log = LogFactory.getLog(SeepIOKkExtendPlug.class);
	private SeepManager seepManager;
	private QiangsSeepManager qiangsSeepManager;
	

	public void setQiangsSeepManager(QiangsSeepManager qiangsSeepManager) {
		this.qiangsSeepManager = qiangsSeepManager;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return "seepIOKkExtendPlug";
	}

	@Override
	public String getLabel() {
		// TODO Auto-generated method stub
		return "Seep模块流程表单使用IO号";
	}

	public ApplicationCategoryEnum getAppName() {
		return ApplicationCategoryEnum.form;
	}

	
	
	// 发起前事件
	public WorkflowEventResult onBeforeStart(WorkflowEventData data) {
		WorkflowEventResult result = new WorkflowEventResult();
		FormBean formBean = (FormBean) data.getBusinessData().get("formBean");
		Map<String, Object> dataMap = data.getBusinessData();
		FormDataMasterBean formDataMasterBean = (FormDataMasterBean) data.getBusinessData().get("formDataBean");
		FormTableBean formData = formDataMasterBean.getFormTable();// 获取表单bean对象
		//查询底表IO号
		try {
			FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
			FormBean formByFormCode = formApi4Cap4.getFormByFormCode(Constants.IOformCode);
			FormTableBean formTableBean = formByFormCode.getMasterTableBean();
			if(StringUtils.isNotBlank(MapUtils.getString(dataMap, "IO号"))){
				log.info("seepkk-IO号已产生，不用重新生成"+MapUtils.getString(dataMap, "IO号"));
				return result;
			}
			
			StringBuilder sql = new StringBuilder();
			sql.append("select top 1  "+formTableBean.getFieldBeanByDisplay("IO号").getName()+" from "+formTableBean.getTableName());
			sql.append(" left outer join CTP_ENUM_ITEM enum on enum.id="+formTableBean.getFieldBeanByDisplay("IO号状态").getName());
			sql.append(" where enum.SHOWVALUE='未使用' and "+formTableBean.getFieldBeanByDisplay("公司代码").getName()+" = "+dataMap.get("公司代码")+" order by "+formTableBean.getFieldBeanByDisplay("IO号").getName()+" asc");
			log.info("seepkk-IO号查询SQL："+sql);
			List<Map> datList = JdbcAgentUtil.doQuery(sql.toString(), null);
			if(datList.isEmpty()){ 
				result.setAlertMessage("无可用IO号，请维护！");
				return result; 
			}
			//回填表单数据，回填时字段必须是浏览状态 
			String ioNum=MapUtils.getString(datList.get(0), formTableBean.getFieldBeanByDisplay("IO号").getName());
			formDataMasterBean.addFieldValue(formBean.getFieldBeanByDisplay("IO号").getName(),ioNum);
			//跟新IO底表状态
			log.info("seepkk:"+qiangsSeepManager.getEnumItemIdByName("IO号状态", "已使用", null));
			StringBuilder updatesql = new StringBuilder();
			updatesql.append("update "+formTableBean.getTableName()+"  set  "+formTableBean.getFieldBeanByDisplay("IO号状态").getName()+" ="+qiangsSeepManager.getEnumItemIdByName("IO号状态", "已使用", null));
			updatesql.append(" where "+formTableBean.getFieldBeanByDisplay("IO号").getName()+" = "+ioNum);
			List updatelist=new ArrayList<>();
			JdbcAgentUtil.doUpdateOrInsert(updatesql.toString(), updatelist);
		} catch (Exception e) {
			log.error("seepkk-生成IO号异常", e);
			result.setAlertMessage("seepkk-生成IO号异常");
			return result;
		}
		return result;
	}
	//发起事件
    public void onStart(WorkflowEventData data){
    	
    }
	
  //处理事件
    public void onFinishWorkitem(WorkflowEventData data){
    	
    }
	public SeepManager getSeepManager() {
		return seepManager;
	}

	public void setSeepManager(SeepManager seepManager) {
		this.seepManager = seepManager;
	}
}